/*	kd:
	
	I'll hack the inventory bar a little. Not that anybody would mind. This will
	make it look a bit smoother.
	
	*/

struct Gc_InvBarControl {
	private Inventory				old_inv;
	private Inventory				inv;
	private Inventory				first_inv;
	private bool					was_shown;
	private bool					is_shown;
	
	void BeginCheck (PlayerPawn po, bool is_bar_shown = false) {
		// kd: The inventory bar is shown if you use invprev or invnext. We have
		// no access to that, but that's no problem. If the selected inventory
		// changes from a valid one to another, we'll show it.
		
		old_inv		= inv;
		inv			= po.invsel;
		first_inv	= po.FirstInv();
		was_shown	= is_shown;
		is_shown	= is_bar_shown;
	}
	
	bool HasSelectionChanged () const {
		return inv != NULL && old_inv != NULL && inv != old_inv;
	}
	
	bool JustActivated () const {
		return is_shown && !was_Shown;
	}
	
	bool JustDeactivated () const {
		return !is_shown && was_shown;
	}
	
	bool IsShown () const {
		return is_shown;
	}
	
	Inventory Selection () const {
		return inv;
	}
	
	Inventory OldSelection () const {
		return old_inv;
	}
	
	bool IsScrollLeft () const {
		return inv.NextInv() == old_inv || (old_inv == first_inv && !inv.NextInv());
	}
	
	bool IsScrollRight () const {
		return old_inv.NextInv() == inv || (inv == first_inv && !old_inv.NextInv());;
	}
}

struct Gc_InvBar {
	private Array <Inventory>		inv;
	Array <TextureId>				sprite;
	Array <int>						amount;
	Array <double>					box_width;
	Array <double>					box_height;
	
	int								count;
	int								selection;
	int								items_shown;
	int								real_items_shown;
	int								draw_index;
	vector2							box_size;
	double							x_offset;
	
	const fade_tics					= 7;
	FadeTick						fade_t;
	
	void Init (
	int 	items_shown_	= 7,
	vector2	box_size_		= (32, 32)) {
		items_shown = max(1, items_shown_);
		box_size	= box_size_;
	}
	
	void FillFromPlayer (PlayerPawn po) {
		inv.clear();
		sprite.clear();
		box_width.clear();
		box_height.clear();
		amount.clear();
		
		let i = 0;
		
		for(
		let cur_inv = po.FirstInv();
		cur_inv != NULL;
		cur_inv = cur_inv.NextInv()) {
			inv.push(cur_inv);
			amount.push(cur_inv.amount);
			sprite.push(0);
			sprite [max(0, sprite.size() - 1)] = cur_inv.icon;
			
			let sprite_size = TexMan.GetScaledSize(cur_inv.icon);
			box_width.push(min(box_size.x, sprite_size.x));
			box_height.push(min(box_size.y, sprite_size.y));
			
			if(cur_inv == po.invsel) {
				selection = i;
			}
			
			i++;
		}
		
		count = inv.size();
		
		if(count < 1) {
			real_items_shown	= 0;
			x_offset			= 0;
			draw_index			= 0;
			return;
		}
		
		real_items_shown	= min(max(0, ((count + 2) / 2) * 2 - 1), items_shown);
		x_offset			= -0.5 * real_items_shown * box_size.x;
		draw_index			= (selection + int(0.5 * real_items_shown)) % count;
	}
	
	int ItemsShown () const {
		return real_items_shown;
	}
	
	void Show () {
		fade_t.ContinueTo(fade_tics);
	}
	
	void Hide () {
		fade_t.ContinueTo(0);
	}
	
	const scroll_tics				= 4;
	FadeTick						scroll_t;
	
	void ScrollLeft () {
		scroll_t.To(-scroll_tics, from: 0);
	}
	
	void ScrollRight () {
		scroll_t.To(scroll_tics, from: 0);
	}
	
	void ForcedScroll () {
		scroll_t.Force(0);
	}
	
	void NextState () {
		fade_t.Next();
		scroll_t.Next();
		
		if(scroll_t.IsDone()) {
			scroll_t.Force(0);
		}
	}
	
	double HorOffset (double anim_f = 0) const {
		let t = scroll_t.Now();
		
		if(t < 0) {
			return x_offset + box_size.x * (anim_f + 1);
		}
		
		else if(0 < t) {
			return x_offset + box_size.x * (anim_f - 1);
		}
		
		return x_offset;
	}
	
	double Alpha (double hor_offset) const {
		return 1 - abs(hor_offset / (0.5 * real_items_shown * box_size.x));
	}
	
	int ItemCount () const {
		return count;
	}
}
